<?php
!defined('P_W') && exit('Forbidden');
class PW_House_ImageTypeDB extends BaseDB {
	var $_tableName = "pw_house_imagetype";
	function getAll() {
		$temp = array();
		$rs = $this->_db->query('SELECT * FROM '.$this->_tableName.' ORDER BY sub,position');
		while ($rt = $this->_db->fetch_array($rs)) {
			$temp[$rt['id']] = $rt;
		}
		return $temp;
	}
	function getTopTypes() {
		$temp = array();
		$rs = $this->_db->query('SELECT * FROM '.$this->_tableName.' WHERE sub=0');
		while ($rt = $this->_db->fetch_array($rs)) {
			$temp[$rt['id']] = $rt;
		}
		return $temp;
	}
	function getSecondTypes($type) {
		$type = (int) $type;
		if (!$type) return null;
		$temp = array();
		$rs = $this->_db->query('SELECT * FROM '.$this->_tableName.' WHERE sub='.pwEscape($type));
		while ($rt = $this->_db->fetch_array($rs)) {
			$temp[$rt['id']] = $rt;
		}
		return $temp;
	}
	function get($id) {
		$temp = $this->_db->get_one('SELECT * FROM '.$this->_tableName.' WHERE id='.pwEscape($id));
		return $temp;
	}
	function getByName($name) {
		$temp = $this->_db->get_one('SELECT * FROM '.$this->_tableName.' WHERE name='.pwEscape($name));
		return $temp;
	}
	function insert($array) {
		$array	= $this->_checkData($array);
		if (!$array || !$array['name']) {
			return null;
		}
		$this->_db->update('INSERT INTO '.$this->_tableName.' SET '.pwSqlSingle($array,false));
		return $this->_db->insert_id();
	}
	function update($id,$array){
		$array	= $this->_checkData($array);
		if (!$array) return null;
		$this->_db->update('UPDATE '.$this->_tableName.' SET '.pwSqlSingle($array,false).' WHERE id='.pwEscape($id));
	}
	function delete($id) {
		$this->_db->update('DELETE FROM '.$this->_tableName.' WHERE id='.pwEscape($id));
	}
	
	function getStruct() {
		return array('id','name','sub','position','ifuse','ifsystem');
	}

	function _checkData($data){
		if (!is_array($data) || !count($data)) return null;
		$data = $this->_checkAllowField($data,$this->getStruct());
		//$data = $this->_serializeData($data);
		if ($data['name'] && strlen($data['name'])>25) {
			return null;
		}
		return $data;
	}
	function _serializeData($data) {
		//if (isset($data['loops']) && is_array($data['loops'])) $data['loops'] = serialize($data['loops']);
		return $data;
	}

	function _unserializeData($data) {
		//if ($data['loops']) $data['loops'] = unserialize($data['loops']);
		return $data;
	}
}
?>